<?php
declare (strict_types=1);

namespace app\user\model\teacher;

use app\common\logic\Attach;
use app\common\model\BaseModel;
use app\school\logic\school\School;
use app\user\model\user\Account as User;
use think\model\concern\SoftDelete;
use think\model\relation\BelongsTo;

// use app\admin\event\subscribes\teacher\Teacher as TeacherSubscribe;

/**
 * 定义讲师数据模型
 */
class Teacher extends BaseModel
{
    use SoftDelete;

    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'teacher';

    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 设置隐藏字段
     *
     * @var array
     */
    protected $hidden = ['create_time', 'update_time', 'delete_time'];

    /**
     * 定义字段类型
     *
     * @var array
     */
    protected $type = [
        'id'               => 'integer',
        'status'           => 'integer',
        'fullcategorypath' => 'array',
    ];

    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';
    // 定义全局的查询范围    PS:由于数据更新时，会走全局，导致更新失败，此处禁用
    // protected $globalScope = ['status'];
    protected static $statusArr = [
        0 => '已禁用',
        1 => '已认证',
        2 => '待审核',
        3 => '认证驳回',
    ];

    public static function getStatusText(): array
    {
        return self::$statusArr;
    }

    /**
     * 新增数据事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-03
     */
    public static function onBeforeInsert($model)
    {
        $model->status = 2;
    }

    /**
     * 设置状态查询条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-10
     *
     * @param     $query
     * @param int $status
     */
    public function scopeStatus($query, int $status = 1)
    {
        $query->where('__TABLE__.status', $status);
    }

    /**
     * 设置删除查询条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-10
     *
     * @param    [type]                         $query  [description]
     */
    public function scopeUndelete($query)
    {
        $query->whereNull('__TABLE__.delete_time');
    }

    /**
     *  获取用户状态显示文字
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-03
     *
     * @param $value
     * @return string
     */
    public function getStatusTextAttr($value): string
    {
        return self::$statusArr[$value];
    }

    public function getIDCardAttr($value): string
    {
        return substr_replace($value, '**************', 0, 14);
    }

    public function schoolName(): BelongsTo
    {
        return $this->belongsTo(School::class, 'mhm_id', 'id')->bind(['school_name' => 'title']);
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function avatar(): BelongsTo
    {
        // 返回的是avatar的附件信息
        return $this->belongsTo(User::class, 'user_id')->bind(['avatar' => 'avatar_url']);
    }

    public function teacherAvatar(): BelongsTo
    {
        // 返回的是avatar的附件信息
        return $this->belongsTo(User::class, 'user_id')->with(['avatar_url'])->bind(['avatar' => 'avatar_url']);
    }

    public function teacherName(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id')->bind(['title' => 'nick_name']);
    }

    public function signature(): BelongsTo
    {
        return $this->belongsTo(User::class)->bind(['signature']);
    }

    public function levelText(): BelongsTo
    {
        return $this->belongsTo(TeacherLevel::class, 'level_id')->bind(['level_text' => 'title']);
    }

    public function IDcardPositiveUrl(): BelongsTo
    {
        return $this->belongsTo(Attach::class, 'IDcard_positive')->bind(['IDcard_positive_url' => 'fileurl']);
    }

    public function IDcardSideUrl(): BelongsTo
    {
        return $this->belongsTo(Attach::class, 'IDcard_side')->bind(['IDcard_side_url' => 'fileurl']);
    }

    public function certificationUrl(): BelongsTo
    {
        return $this->belongsTo(Attach::class, 'certification')->bind(['certification_url' => 'fileurl']);
    }

    /**
     * 讲师所属机构
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-08-11
     */
    public function school(): BelongsTo
    {
        return $this->belongsTo(School::class, 'mhm_id');
    }

    /**
     * 获取图标事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2022-09-21
     */
    public function iconUrl(): BelongsTo
    {
        return $this->belongsTo(TeacherLevel::class, 'level_id')->with('icon_url')->bind(['icon_url']);
    }

}
